1 // Accepted. Ternary search.
24 #define foreach(x, v) for (typeof (v).begin() x=(v).begin(); x !=(v).end(); ++x)
25 #define For(i, a, b) for (int i=(a); i<(b); ++i)
26 #define D(x) cout << #x " is " << x << endl
28 const double eps
= 1e-9;
31 double running_speed
[MAXN
], biking_speed
[MAXN
];
34 double margin(double x
, int t
) {
35 double me
= x
/ running_speed
[n
- 1] + (t
- x
) / biking_speed
[n
- 1];
37 for (int i
= 0; i
< n
- 1; ++i
) {
38 them
= min(them
, x
/ running_speed
[i
] + (t
- x
) / biking_speed
[i
]);
44 double best_running_length(int t
) {
45 double left
= 0, right
= t
;
46 for (int i
= 0; i
< 150; ++i
) {
47 double r1
= (2 * left
+ right
) / 3;
48 double r2
= (left
+ 2 * right
) / 3;
50 if (margin(r1
, t
) < margin(r2
, t
)) {
56 return (left
+ right
) / 2;
63 for (int i
= 0; i
< n
; ++i
) {
64 cin
>> running_speed
[i
] >> biking_speed
[i
];
65 assert(running_speed
[i
] > eps
and biking_speed
[i
] > eps
);
68 double r
= best_running_length(t
);
69 double m
= margin(r
, t
);
71 printf("The cheater cannot win.\n");
73 printf("The cheater can win by %.0lf seconds with r = %.2lfkm and k = %.2lfkm.\n", m
* 3600, r
, t
- r
);